package com.glory.dgcpcpda.activity.mfg;

import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.AddCarrierDataAdapter;
import com.glory.dgcpcpda.adapter.MaterielListAdapter;
import com.glory.dgcpcpda.adapter.PartListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.AdrefListEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.MaterielListEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: DAWorkBeginActivity
 * Author: XJH
 * Date: 2020/5/21 9:05
 * Description:开始作业DA工序
 * History:
 */
public class WorkBackUpActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etComment)
    EditText etComment;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_back_up;
    }

    @Override
    protected void initView() {
        tvTitle.setText("批次退步");
        setMainLayout(llMain);
        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(WorkBackUpActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    String state = response.getLOT().getSTATE();
                    if(state.equals("正在作业")){
                        state = state + " - " + response.getLOT().getEQUIPMENTID();
                    }
                    tvLotState.setText(state);
                    tvStepDesc.setText(response.getLOT().getSTEPDESC());
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t,"LOTQUERY");
                }

            });
        });
    }

    private void clean(){
        etLot.setText("");
        tvLotState.setText("");
        tvStepDesc.setText("");
        etComment.setText("");
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {

        if (tvLotState.getText().toString().contains("正在作业")){
            showConfirmDialog("批次正在作业，请直接操作出站功能");
            return;
        }

        if(etComment.getText().toString().isEmpty()){
            showConfirmDialog("备注信息不能为空，请检查！");
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().getResponse().getHeader().getRESULT().equals("SUCCESS")){
                    showConfirmDialog(etLot.getText().toString()+"退步成功");
                    clean();
                }else {
                    showConfirmDialog(response.body().getResponse().getHeader().getRESULTMESSAGE());
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"TRANSFERMES");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","BACKUP");
        map.addProperty("COMMENT",etComment.getText().toString());
        map.addProperty("LOTID",etLot.getText().toString());
        NetUtils.requestPostNet("CPC.LOTBACKUP","TRANSFERMES",WorkBackUpActivity.this,map,callBack);
    }


}
